﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ReportForumBehaviour : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ReportForumBehaviour_Title;
            //this.Title = Resources.Resource.ReportForumBehaviour_Title + " - " + GND.ReturnAppTitle();
            
            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            if (!IsPostBack)
            {

                if (Request.QueryString["Answer"] != null)
                {
                    MessagePanel.Visible = true;
                    this.MessageLabel.Text = Request.QueryString["Answer"].ToString();
                }
                string PostId = null;
                
                if (Request.QueryString["PostId"] != null)
                {
                    PostId = Request.QueryString["PostId"].ToString();
                    PostIdHiddenField.Value = PostId;
                }
                else
                    Response.Redirect("~/ForumHomePage.aspx");
                string ReportedUserId = null;
                ReportedUserId = GND.ReadNivdalStringData("PostId", "Nivdal_GetForumPostPostedByUser", PostId);
                ReportedUserIdHiddenField.Value = ReportedUserId;
                //if (Request.QueryString["ReportedUserId"] != null)
                //    ReportedUserId = Request.QueryString["ReportedUserId"].ToString();
                //else
                //    Response.Redirect("~/ForumHomePage.aspx");
                String UserName = GND.ReadNivdalStringData("@UserId", "Nivdal_GetUserNameById", ReportedUserId);
                this.ReportAgainstHyperLink.Text = UserName;
                this.ReportAgainstHyperLink.NavigateUrl = "UserPage.aspx?Id=" + ReportedUserId;
                this.PostTextLabel.Text = GND.ReadNivdalStringData("@PostId", "Nivdal_ForumGetPostText", PostId);
            }
        }
        
        protected void SubmitReportButton_Click(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Submitter = ou.UserId;

            string PostId = PostIdHiddenField.Value;

            string ReportedUserId = ReportedUserIdHiddenField.Value;
            
            string ReportText = this.ReportNoteTextArea.Text.Trim();
            
            string ThreadId = GND.ReadNivdalIntData("@PostId", "Nivdal_ForumGetThreadByPost", PostId);

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_ReportInappropriateBehaviour";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_Submitter = new SqlParameter();
            dbParam_Submitter.ParameterName = "@Submitter ";
            dbParam_Submitter.Value = Submitter;
            dbParam_Submitter.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Submitter);

            SqlParameter dbParam_PostId = new SqlParameter();
            dbParam_PostId.ParameterName = "@PostId";
            dbParam_PostId.Value = PostId;
            dbParam_PostId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_PostId);

            SqlParameter dbParam_ReportedUser = new SqlParameter();
            dbParam_ReportedUser.ParameterName = "@ReportedUser";
            dbParam_ReportedUser.Value = ReportedUserId;
            dbParam_ReportedUser.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ReportedUser);

            SqlParameter dbParam_ReportText = new SqlParameter();
            dbParam_ReportText.ParameterName = "@ReportText";
            dbParam_ReportText.Value = ReportText;
            dbParam_ReportText.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ReportText);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                //Response.Write("ThreadDetails.aspx?ThreadId=" + ThreadId + "&PostId=" + PostId + "&Answer=" + dbParam_Answer.Value.ToString());
                Response.Redirect("ThreadDetails.aspx?ThreadId=" + ThreadId + "&PostId=" + PostId + "&Answer=" + dbParam_Answer.Value.ToString());
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);

        }
    }
}